﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2011

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using NetBay.Core.BusinessEntities;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Applications.Web.RioWCFWorkflow;
using Rio.Framework.Pages.Events;
using Rio.Framework.Pages.Workflow.Models;
using System.ComponentModel;

namespace Rio.Framework.Pages.Workflow.Actions
{
    [Export]
    public abstract partial class WorkflowButtonAction : IDisposable, INotifyPropertyChanged
    {
        #region Events

        public event IndexedFilesEventHandler IndexPanelClosed;
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Fields

        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(Uri), typeof(WorkflowButtonAction), null);

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(WorkflowButtonAction), null);

        #endregion

        #region Properties

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            protected set { SetValue(TextProperty, value); }
        }

        public Uri Image
        {
            get { return (Uri)GetValue(ImageProperty); }
            protected set { SetValue(ImageProperty, value); }
        }

        public WorkflowTypeAction UiTypeAction { get; protected set; }

        public NetBay.Core.BusinessEntities.Workflow.Rules.Action RulesAction { get; set; }

        public bool IsGlobalAction { get; protected set; }

        public WorkflowInstance WorkflowInstance { get; set; }

        #endregion

        #region Constructors

        protected WorkflowButtonAction()
        {
            InitializeComponent();

            Name = Guid.NewGuid().ToString("N");

            Initialize();
        }

        #endregion

        protected virtual void Initialize()
        {
            Command = new RelayCommand(ActionClicked);
        }

        protected void ExecuteAction(List<Parameter> parameters = null)
        {
            if (!RulesAction.IsOption)
            {
                //Send action to WorkflowManager
                Messenger.Default.Send(new WorkflowNotificationMessage(this, string.Empty)
                                           {
                                               ActionId = RulesAction.Identifier,
                                               IsGlobalAction = IsGlobalAction,
                                               UiActionType = UiTypeAction,
                                               Parameters = parameters
                                           },
                                           WorkflowInstance.Identifier);
            }
        }

        protected virtual void ActionClicked()
        {
            ExecuteAction();
        }

        protected void ActivateIndexPanel()
        {
            /*
             * Send a message with a parameter (List<Document>) to pass in FolderPage -> FlowIndexPanel
             * And a callback to execute the action
             */

            var message = new NotificationMessageAction<IndexedFilesEventArgs>(
                "activateflowindexpanel",
                callbackMessage =>
                {
                    if (callbackMessage != null)
                    {
                        OnIndexPanelClosed(this, callbackMessage);
                    }
                });
            Messenger.Default.Send(message, WorkflowInstance.Identifier);
        }

        protected void OnIndexPanelClosed(object sender, IndexedFilesEventArgs e)
        {
            if (IndexPanelClosed != null)
            {
                IndexPanelClosed(this, e);
            }
        }

        public virtual void Dispose() { }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}